package org.correct.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Calendar;

import org.correct.config.Config;

public class Logger {
	private PrintWriter w;
	
	public Logger() throws FileNotFoundException {
		File f = new File(Config.LOG_BASE);
		if(!f.exists())
			f.mkdirs();
		
		Calendar time = Calendar.getInstance();
		w = new PrintWriter(Config.LOG_BASE + "/log-" + time.getTimeInMillis());
	}
	
	public Logger(String logFolderPath) throws FileNotFoundException {
		File f = new File(logFolderPath);
		if(!f.exists())
			f.mkdirs();
		
		Calendar time = Calendar.getInstance();
		w = new PrintWriter(logFolderPath + "/log-" + time.getTimeInMillis());
	}
	
	public void print(String item) {
		Calendar time = Calendar.getInstance();
		w.print(item + "[" + 
				time.get(Calendar.YEAR) + "-" + 
				time.get(Calendar.MONTH) + "-" + 
				time.get(Calendar.DATE)+ "]");
		w.flush();
	}
	
	public void println(String item) {
		w.println(item);
		w.flush();
	}
	
	public void log(String keyword, String url) {
		Calendar time = Calendar.getInstance();
		w.println("q=" + keyword + ",a=" + url + ",t=" + 
				"[" + 
				time.get(Calendar.YEAR) + "-" + 
				time.get(Calendar.MONTH) + "-" + 
				time.get(Calendar.DATE)+ "]");
		w.flush();
	}
}
